//
//  ProfileController.m
//  QQplayground
//
//  Created by zhangxy on 2018/5/21.
//  Copyright © 2018年 zhang. All rights reserved.
//

#import "ProfileController.h"
#import "ProfileCell.h"
#import "UserHeaderCell.h"
#import <MJRefresh.h>
#import "SettingController.h"
#import "UserInfoController.h"
#import "LoginController.h"
#import "CollectController.h"

#define cellIdentifier @"ProfileCell"

@interface ProfileController ()

@property(nonatomic,strong)UITableView      *listTable;
@property(nonatomic,strong)UserHeaderCell   *headerCellView;
@property(nonatomic,strong)NSArray   *titleArr;
@property(nonatomic,strong)User *user;

@end

@implementation ProfileController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBarHidden = YES;
    [self createTitleMenu];
    self.menuLeftButton.hidden = YES;
    [self.menuTitleButton setTitle:@"我的" forState:0];
    
    // Do any additional setup after loading the view.
    [self createTableView];
    [_listTable reloadData];
}


-(void)createTableView{
    _titleArr = @[@[@{@"title":@"我的收藏",@"icon":@"icon_fav",@"code":@"1"}],
                @[@{@"title":@"商店",@"icon":@"icon_shopping",@"code":@"2"}],
                @[@{@"title":@"每日签到",@"icon":@"icon_qd",@"code":@"3"},
                  @{@"title":@"每日任务",@"icon":@"icon_task",@"code":@"4"}],
                @[@{@"title":@"意见反馈",@"icon":@"icon_jy",@"code":@"5"},
                    @{@"title":@"分享给好友",@"icon":@"icon_myshare",@"code":@"6"},
                   @{@"title":@"设置",@"icon":@"icon_mysetting",@"code":@"7"}]];
    
    _listTable=[self.view createTableView:self cell:cellIdentifier];
    _listTable.frame = CGRectMake(0, NavBarHeight, ScreenWidth, ScreenHeight-NavBarHeight);
    _listTable.backgroundColor = [UIColor whiteColor];
    
    if (iOS7) {
        _listTable.backgroundView = nil;
    }
    
    _listTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    _listTable.tableHeaderView = [self headerView];
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadBegin];
    }];
    _listTable.header = header;
    
    _user = [UserUtil getUser];
    [self loadBegin];
}

-(void)loadBegin{
    [self loadMoreData];
}


/**
 加载更多
 */
-(void)loadMoreData{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [NetworkUtil POST:API_UserInfo parameters:@{@"user_id":convertIntToString((int)_user.user_id),@"is_all":@"1"} success:^(id responseObject,NSDictionary *dictionary) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if([self.listTable.header isRefreshing]){
            [self.listTable.header endRefreshing];
        }
        
        DLog(@"%@",dictionary);
        
        if ([dictionary[@"status"] integerValue] == kNetworkLoginCode) {
            self.user = [[User alloc] initWithDictionary:dictionary[@"data"]];
            [self.listTable reloadData];
        }
        else{
            [PopUtil showMessage:dictionary[@"message"] positionType:PopPositionTypeMiddle];
        }
    } failure:^(NSError *error,NSString *message) {
        if([self.listTable.header isRefreshing]){
            [self.listTable.header endRefreshing];
        }
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}


- (UIView *)headerView
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 100)];
    [headerView setBackgroundColor:UIColorFromRGB(BgTitleColor)];
    
    User *user = [UserUtil getUser];
    if(ZC_iPhoneX){
        _headerCellView = [[NSBundle mainBundle]loadNibNamed:@"UserHeaderCell" owner:nil options:nil].firstObject;
    }else{
        _headerCellView = [[NSBundle mainBundle]loadNibNamed:@"UserHeaderCell" owner:nil options:nil].firstObject;
    }
    [_headerCellView setFrame:headerView.bounds];
    [_headerCellView dataToView:user];

    [headerView addSubview:_headerCellView];
    [headerView addTapGestureRecognizerWithTarget:self action:@selector(headerClick:)];
    return headerView;
}
-(void)headerClick:(UIGestureRecognizer *) tap{
    if([UserUtil getUser].isOnline){
        UserInfoController *vc = [[UserInfoController alloc] init];
        vc.user_id = _user.user_id;
        [self openPushVC:vc];
    }else{
        LoginController *profileVC = [[LoginController alloc] init];
        
        //将vc设置为navi vc的根控制器
        BaseNaviController *naviVC = [[BaseNaviController alloc] initWithRootViewController:profileVC];
        [self presentViewController:naviVC animated:YES completion:^{
            
        }];
    }
}



#pragma mark UITableView delegate Start
// 返回section数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _titleArr.count;
}

// 返回section高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section==0){
        return 0;
    }else{
        return 10;
    }
}

// 返回section 的View
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectZero];
    if(section == 0){
        [headerView setBackgroundColor:UIColor.clearColor];
        [_headerCellView dataToView:[UserUtil getUser]];
    }else{
        [headerView setBackgroundColor:UIColorFromRGB(BgSystemColor)];
        [headerView setFrame:CGRectMake(0, 0, ScreenWidth, 10.0f)];
        
    }
    return headerView;
}

// 返回section下得行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(_titleArr.count == 0){
        return 0;
    }
    NSArray *items = _titleArr[section];
    return items.count;
}

// cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ProfileCell *cell = (ProfileCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[ProfileCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    NSArray *items = _titleArr[indexPath.section];
    [cell dataToView:items[indexPath.row]];
    return cell;
}


// table 行的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44.0f;
}

// table 行的点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //    VipModel *model=[_listArray objectAtIndex:indexPath.section];
    NSDictionary *item = _titleArr[indexPath.section][indexPath.row];
    int code = [item[@"code"] intValue];
    
    switch (code) {
        case 1:{
            CollectController *vc = [[CollectController alloc] init];
            [self openPushVC:vc];
            break;
        }case 7:{
            SettingController *vc = [[SettingController alloc] init];
            [self openPushVC:vc];
        }
            
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
